<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('审核环保承诺书')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">

    <style>
        thead tr th, tbody tr td input {
            text-align: center;
        }
    </style>

</head>
<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="form-group">
            <div class="col-sm-8" style="margin-left: 16px">
                <input class="btn btn-primary" type="button" onclick="save()" value="确定">
                <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
            </div>
        </div>
        <div class="col-sm-12 ibox-content" style="margin-top: 10px">
            <table class="table table-bordered text-nowrap">
                <thead>
                <tr>
                    <th>高度关注物质</th>
                    <th>中文名称</th>
                    <th>英文名称</th>
                    <th>化学文摘号</th>
                    <th>是否含有</th>
                    <th>物质含量（ppm）</th>
                    <th>用途描述</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="entity : ${svhcItemEntities} ">
                    <td>
                        <input type="text" th:value="${entity.pkId}" hidden>
                        <input type="text" th:value="${entity.headId}" hidden>
                        <input class="form-control" type="text" th:value="${entity.svhc}" readonly>
                    </td>
                    <td>
                        <input class="form-control" type="text" th:value="${entity.zhName}" readonly>
                    </td>
                    <td>
                        <input class="form-control" type="text" th:value="${entity.enName}" readonly>
                    </td>
                    <td>
                        <input class="form-control" type="text" th:value="${entity.casNo}" readonly>
                    </td>
                    <td>
                        <select class="form-control" sysDict="SYF2" emptyFirst="true" required
                                onchange="selChange(this)"></select>
                    </td>
                    <td>
                        <input class="form-control" type="number">
                    </td>
                    <td>
                        <input class="form-control" type="text">
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<script type="text/javascript">

    $(function () {
        //初始化所有字典数据
        initDictDataSel();
    });

    //是否含有，否则将物质含量清空 readonly
    function selChange(obj) {
        if ($(obj).val() == "0") {
            //物质含量清空，只读
            $(obj).parent().next().children().first().val("");
            $(obj).parent().next().children().first().attr("readonly", "readonly");
            $(obj).parent().next().children().first().removeAttr("required");
        } else {
            $(obj).parent().next().children().first().removeAttr("readonly");
            $(obj).parent().next().children().first().attr("required", "required");
        }
    }

    //保存提交的数据
    function save() {
        //得到行项目数据
        var trs = $("tbody").find("tr");

        //标志变量，填写内容不正确或漏填不执行
        var flag = true;

        var datas = [];
        //将每行数据封装成对象存入datas
        trs.each(function (index, tr) {
            var SvhcVendorVO = {};
            //svhc_item表id
            SvhcVendorVO.svhcItemId = $(tr).find("input")[0].value;
            //svhc_head表id
            SvhcVendorVO.svhcHeadId = $(tr).find("input")[1].value;
            //是否包含关注物质
            SvhcVendorVO.contain = $(tr).find("select")[0].value;
            //物质含量
            SvhcVendorVO.ppm = $(tr).find("input")[6].value;
            if (SvhcVendorVO.contain == undefined || SvhcVendorVO.contain == null || SvhcVendorVO.contain == '') {
                flag = false;
                Dialog.errorMsg("请选择 是否含有！");
                return;
            } else if (SvhcVendorVO.contain == 1 && (SvhcVendorVO.ppm == '' || isNaN(SvhcVendorVO.ppm) || SvhcVendorVO.ppm <= 0)) {
                flag = false;
                Dialog.errorMsg("请输入正确的物质含量！");
                return;
            }
            //用途描述
            SvhcVendorVO.description = $(tr).find("input")[7].value;
            datas.push(SvhcVendorVO);
        });

        if (!flag) {
            return;
        }
        layer.load(1, {shade: [0.3, '#393D49']});
        $.ajax({
            url: ctxPath + "/svhc/saveVendorEdit",
            type: 'POST',
            data: JSON.stringify(datas), // 上传formdata封装的数据包
            dataType: 'JSON',
            cache: false, // 不缓存
            processData: false, // jQuery不要去处理发送的数据
            contentType: false, // jQuery不要去设置Content-Type请求头
            success: function (data) {
                layer.closeAll();
                if (data.code == 0) {
                    Dialog.msg("操作成功!");
                    parent.refresh();
                    Dialog.closeFrame(window.name);
                } else {
                    Dialog.errorMsg("操作失败，请重试!");
                }
            }
        });
    }

</script>

</body>
</html>